<?php
namespace BoxApi\Controller;

use Think\Controller;

class EventsController extends CommonController {

	public function _initialize() {
		parent::_initialize();
		if ($user_id = I('user_id', "0", "intval")) {
			$this->user_id = $user_id;
		}
	}

	public function getUserInfo() {
		$info = BoxModel("system_member")->load($this->user_id);
		$user_info = array();
		if ($info) {
			$user_info['nickname'] = $info['nickname'];
			$user_info['user_id'] = $info['user_id'];
			$user_info['headimg'] = $info['headimg'];
		}
		exit(json_encode($user_info));
	}

public function getUUID() {
		$openid = I('open_id');
		if (!$openid) {
			exit(json_encode(array('error' => 1, 'msg' => 'openid is required')));
		}
		//dump(urldecode(I('avatar')));
		$row = array(
			'openid' => $openid,
			'headimg' => urldecode(I('avatar')),
			'nickname' => urlencode(I('nickname')),
			'store_id' => 134,
			'type' => 'puthinkEvents',
		);
		$model = BoxModel("system_member");
		$user_info = $model->where(array('openid' => $openid))->find();
		if (!$user_info) {
			$row['user_name'] = 'user_name_' . $openid . "_" . great_rand(5);
			$row['password'] = md5($user_name);
		} else {
			$row['user_id'] = $user_info['user_id'];
		}
		$user_id = $model->create_member($row);
		$user_info = $model->load($user_id);
		if ($user_info['user_id']) {
			$uuid = $model->get_uuid($user_info['user_id']);
			$user_info['uuid'] = $uuid;
		}
		if ($user_info['user_id'] && $user_info['uuid']) {
			exit(json_encode(array('error' => 0, 'data' => $user_info)));
		} else {
			exit(json_encode(array('error' => 2, 'msg' => 'openid is required')));
		}

	}
	public function getUUID1() {
		$openid = I('open_id');
		if (!$openid) {
			exit(json_encode(array('error' => 1, 'msg' => 'openid is required')));
		}
		//dump(urldecode(I('avatar')));
		$model = BoxModel("system_member");
		$user_info = $model->where(array('openid' => $openid))->find();
		if (!$user_info) {
			$user_name = 'user_name_' . $openid . "_" . great_rand(5);
			$row = array(
				'openid' => $openid,
				'user_name' => $user_name,
				'password' => md5($user_name),
				'headimg' => urldecode(I('avatar')),
				'nickname' => urlencode(I('nickname')),
				'store_id' => 134,
				'type' => 'puthinkEvents',
			);
			$user_id = $model->create_member($row);
			$user_info = $model->load($user_id);

		}
		if ($user_info['store_id'] != 134) {
			$model->where(array('user_id' => $user_info['user_id']))->save(array('type' => 'puthinkEvents', 'store_id' => 134));
		}
		if ($user_info['user_id']) {
			$uuid = $model->get_uuid($user_info['user_id']);
			$user_info['uuid'] = $uuid;
		}

		if ($user_info['user_id'] && $user_info['uuid']) {
			exit(json_encode(array('error' => 0, 'data' => $user_info)));
		} else {
			exit(json_encode(array('error' => 2, 'msg' => 'openid is required')));
		}

	}

	//当前活动
	public function index() {
		$model = BoxModel("addon_events");
		$eventData = $model->order("id desc")->limit(1)->select();
		$data = $eventData[0];
		$data['picUrl'] = ImgDomain . "/" . $data['pic'];
		$where = array(
			'pid' => $data['id'],
			'type' => 'events',
			'is_show' => 1,
		);
		$orderBy = I('orderBy', "2", "intval");
		if ($orderBy == 1) {
			$order = " good_count desc,id desc ";
		}
		if ($orderBy == 2) {
			$order = " id desc ";
		}
		if ($orderBy == 3) {
			$order = " id ";
		}
		if ($orderBy == 4) {
			$order = " comment_count desc,id desc ";
		}

		$list = BoxModel("sns_topic")->get($where, 10, $order);
		$data['expire_date_limit'] = $data['expire_date'] - time();
		$list['event'] = $data;
		exit(json_encode($list));
	}

	//历史列表
	public function eventList() {
		$model = BoxModel("addon_events");
		$data = $model->get($where, 10, "id desc");
		exit(json_encode($data));

	}

	public function UserInfo() {
		exit(json_encode($this->user_info));
	}

	//活动详情
	public function eventDetail() {
		$model = BoxModel("addon_events");
		$id = I("id", "0", "intval");
		$data = $model->load($id);
		exit(json_encode($data));
	}

	//参加活动帖子
	public function topic() {
		$limit = I('limit', "10", "intval");
		$limit = $limit > 50 ? 50 : $limit;
		$orderBy = I('orderBy', "2", "intval");
		if ($orderBy == 1) {
			$order = " good_count desc ";
		}
		if ($orderBy == 2) {
			$order = " id desc ";
		}
		if ($orderBy == 3) {
			$order = " id ";
		}
		if ($orderBy == 4) {
			$order = " comment_count desc ";
		}
		$where = array(
			'type' => 'events',
			'is_show' => 1,
		);
		if ($pid = I('pid', "0", "intval")) {
			$where['pid'] = $pid;
			$eventsData = BoxModel("addon_events")->load($pid);
		}
		//判断类型
		if ($type = I('type')) {
			switch ($type) {
			//获奖名单
			case 'awards':
				$where['is_best'] = 1;
				break;
			//我发布过的
			case 'publish':
				$where['user_id'] = $this->user_id;
				break;
			//我评论过的
			case 'myComments':
				$count = BoxModel("sns_comment")->where(array('user_id' => $this->user_id, 'type' => 'events'))->count("distinct(pid)");
				$page = setPager($count, $limit);
				$limit = $page['firstRow'] . "," . $page['listRows'];
				$data = BoxModel("sns_comment")->where(array('user_id' => $this->user_id, 'type' => 'events'))->limit($limit)->field("distinct(pid)")->select();

				foreach ($data as $key => $value) {
					$data[$key] = BoxModel("sns_topic")->load($value['pid']);
				}
				$list = array('list' => $data, 'pager' => $page, 'count' => $count);
				exit(json_encode($list));

				break;
			//我赞过的
			case 'myGoods':
				$count = BoxModel("sns_topic_goods")->where(array('user_id' => $this->user_id))->count();
				$page = setPager($count, $limit);
				$limit = $page['firstRow'] . "," . $page['listRows'];
				$data = BoxModel("sns_topic_goods")->where(array('user_id' => $this->user_id))->limit($limit)->field("topic_id")->select();
				foreach ($data as $key => $value) {
					$data[$key] = BoxModel("sns_topic")->load($value['topic_id']);
				}
				$list = array('list' => $data, 'pager' => $page, 'count' => $count);
				exit(json_encode($list));
				break;
			default:
				# code...
				break;
			}

		}
		//

		$data = (array) BoxModel("sns_topic")->get($where, $limit, $order);
		if ($eventsData) {
			$data['event'] = $eventsData;
		}
		exit(json_encode($data));
	}

//我收到的赞
	public function userGoods() {
		$data = BoxModel("sns_topic_goods")->get(array('to_user_id' => $this->user_id), 15);
		foreach ($data['list'] as $key => $value) {
			$data['list'][$key]['user'] = (array) BoxModel("system_member")->where(array('user_id' => $value['user_id']))->find();
			if ($data['list'][$key]['user']['nickname']) {
				$data['list'][$key]['user']['nickname'] = urldecode($data['list'][$key]['user']['nickname']);
			}
			$data['list'][$key]['remind'] = $value['remind'];
			$data['list'][$key]['topic'] = BoxModel("sns_topic")->load($value['topic_id']);
			$data['list'][$key]['time_tran'] = time_tran($value['addtime']);
		}
		if ($this->user_id == $this->user_info['user_id']) {
			BoxModel("SnsNotificationCount")->clear($this->user_id, "goods");
		}
		exit(json_encode($data));
	}

//我参与的活动
	public function userEvents() {
		$count = BoxModel("sns_topic")->where(array('user_id' => $this->user_id, 'is_show' => 1, 'type' => 'events'))->count("distinct(pid)");
		$page = setPager($count, $limit);
		$limit = $page['firstRow'] . "," . $page['listRows'];
		$data = BoxModel("sns_topic")->where(array('user_id' => $this->user_id, 'is_show' => 1, 'type' => 'events'))->limit($limit)->field("distinct(pid)")->select();
		foreach ($data as $key => $value) {
			$data[$key] = BoxModel("addon_events")->load($value['pid']);
		}
		$list = array('list' => $data, 'pager' => $page, 'count' => $count);
		exit(json_encode($list));
	}

	//帖子详情
	public function topicDetail() {
		$id = I('id', "0", "intval");
		$data = BoxModel("sns_topic")->load($id);
BoxModel("sns_topic")->where(array('id' => $id))->setInc("hit_count");
		$data['content'] = strip_tags($data['content']);
		//点赞列表
		$goodList = BoxModel("sns_topic_goods")->get(array('topic_id' => $id), "3", "id desc");
		$data['has_good'] = BoxModel("sns_topic_goods")->where(array('topic_id' => $id, 'user_id' => $this->user_id))->find();
		$member = BoxModel("system_member");
		foreach ($goodList['list'] as $key => $value) {
			$goodList['list'][$key]['user'] = $member->where(array('user_id' => $value['user_id']))->find();
		}
		$data['events'] = BoxModel("addon_events")->load($data['pid']);
		$data['goodList'] = (array) $goodList['list'];
		exit(json_encode($data));
	}

	//我收到的评论
	public function myComment() {
		$data = (array) BoxModel("SnsComment")->get(array('to_user_id' => $this->user_id, 'type' => 'events'), 10, "id desc");
		if ($this->user_id == $this->user_info['user_id']) {
			BoxModel("SnsNotificationCount")->clear($this->user_id, "comment");
		}
		exit(json_encode($data));
	}
	//我的提醒数
	public function notification() {
		$data = BoxModel("SnsNotificationCount")->mycount($this->user_id);
		$variable = array();
		$variable['user_id'] = $this->user_id;
		foreach ($data as $key => $value) {
			$variable[$value['type']] = $value['count'];
		}
		$variable['all'] = intval($variable['comment']) + intval($variable['goods']);

		exit(json_encode($variable));
	}

	//帖子评论
	public function comment() {
		$topic_id = I("id", "0", "intval");
		$data = (array) Logic("BoxSns", "Comment")->get(
			array('topic_id' => $topic_id, 'type' => "events", 'orderBy' => 'id desc', 'limit' => 10)
		);
		exit(json_encode($data));
	}

	//我的奖品
	public function userAwards() {
		$model = BoxModel("addon_events");
		$where = array(
			'is_best' => 1,
			'type' => 'events',
			'is_show' => 1,
			'user_id' => $this->user_id,
		);
		$data = (array) BoxModel("sns_topic")->get($where, 10);
		$list = array();
		foreach ($data['list'] as $key => $value) {
			$data['list'][$key]['events'] = $model->load($value['pid']);
		}
		exit(json_encode($data));
	}

	//用户数据 收到的赞 收到的评论
	public function userStatistics() {
		$data = array();
		//我参与的活动
		$data['events'] = (int) BoxModel("sns_topic")->where(array('user_id' => $this->user_id, 'is_show' => 1, 'type' => 'events'))->count("distinct(pid)");
		//收到的赞
		$data['receiveGoods'] = BoxModel("sns_topic_goods")->where(array('to_user_id' => $this->user_id))->count();
		//收到的评论
		$data['receiveGomments'] = BoxModel("sns_comment")->where(array('to_user_id' => $this->user_id, 'type' => 'events'))->count();
		//我发布过的
		$data['topic'] = BoxModel("sns_topic")->where(array('user_id' => $this->user_id, 'is_show' => 1, 'type' => 'events'))->count();
		//我赞过的
		$data['goods'] = (int) BoxModel("sns_topic_goods")->where(array('user_id' => $this->user_id))->count();
		//我评论过的
		$data['comments'] = (int) BoxModel("sns_comment")->where(array('user_id' => $this->user_id, 'type' => 'events'))->count("distinct(pid)");

		//我的奖品
		$data['awards'] = (int) BoxModel("sns_topic")->where(array('user_id' => $this->user_id, 'is_show' => 1, 'is_best' => 1, 'type' => 'events'))->count();
		//dump(BoxModel("sns_topic"));

		exit(json_encode($data));

	}

//我的地址
	public function address() {
		if (IS_POST) {
			$row = array(
				'phone' => I('phone'),
				'name' => I('name'),
				'address' => I('address'),
			);
			$r = BoxModel("system_member")->where(array('user_id' => $this->user_id))->save($row);
			if ($r !== false) {
				$arr = array(
					'error' => 0,
					'msg' => '成功',
					'userInfo' => $row,
				);
			} else {
				$arr = array(
					'error' => 1,
					'msg' => '保存失败',
				);
			}
			exit(json_encode($arr));
		}
	}

//反馈问题
	public function feedback() {
		if (IS_POST) {
			$msg = trim(I('msg'));
			if ($msg == "") {
				$arr = array(
					'error' => 2,
					'msg' => '请输入反馈信息',
				);
				exit(json_encode($arr));
			}
			$imgList = I('imgList');
			if ($imgList) {
				foreach ($imgList as $one) {
					$msg .= '<img src="' . $one['file_url'] . '" >';
				}
			}
			$row = array(
				'user_id' => $this->user_id,
				'msg' => $msg,
				'addtime' => time(),
			);
			$r = BoxModel("addon_feedback")->add($row);
			if ($r !== false) {
				$arr = array(
					'error' => 0,
					'msg' => '成功',
				);
			} else {
				$arr = array(
					'error' => 1,
					'msg' => '保存失败',
				);
			}
			exit(json_encode($arr));
		}
	}
	//轮播图片
	function swiper() {
		$id = I('id');
		$data = BoxModel("addon_blocks_image")->get(array('blocks_name' => "blocks_" . $id), "", "order_by");
		foreach ($data as $key => $value) {
			$data[$key]['picUrl'] = ImgDomain . "/" . $value['image'];
		}
		//dump(BoxModel("addon_blocks_image"));
		exit(json_encode($data));
	}

	/***********调用微信的js***/
	public function wxJs() {
		$param = BoxModel("addon_wx_config")->load(134);
		$this->wechatObj = new \Common\Org\weiXin($param);
		//dump($_SERVER['HTTP_REFERER']);
		//$url = "http://puxiang.boxphp.me/";
		$url = $_SERVER['HTTP_REFERER'];
		$this->signPackage = $this->wechatObj->GetSignPackage($url);
		echo json_encode($this->signPackage);
	}
	//已经读
	function notificationRead() {
		ignore_user_abort();
		$id = I('id');
		$type = I('type');
		$from_user_id = I('from_user_id', "0", "intval");
		if ($from_user_id > 0) {
			echo json_encode(array('error' => 1, 'msg' => '看别的信息不更新'));
		}
		if ($type == 'comment') {
			$r = BoxModel("sns_comment")->where(array('pid' => $id, 'type' => 'events', 'to_user_id' => $this->user_id))->save(array('remind' => 0));

		}
		if ($type == 'goods') {
			$r = BoxModel("sns_topic_goods")->where(array('topic_id' => $id, 'to_user_id' => $this->user_id))->save(array('remind' => 0));
		}
		echo json_encode(array('error' => 0, 'type' => $type, 'msg' => $r));

	}

}
